<?php

class MenuWidget extends CWidget{
    
    public $root;
    public function run() {
        parent::run();
        $output = "";
        $Menu = Menu::model()->findByPk($this->root);
        $this->loopNode($output, $Menu,null,"");
        $this->render('menu',array('output' => $output));
    }
    
    /**
     * show element
     * @return type 
     */
    private function loopNode(&$output,$Menu,$parent,$prefix,$spear = "<sup>|___</sup>"){
        if($Menu != null){
            $newPrefix = "";
            if($Menu->id != 1 && $parent != null){
                ob_start();
                $this->render('ElementMenu',array('parent' => $parent,'Menu' => $Menu,'prefix' => $prefix,'spear' => $spear,'root' => $this->root));
                $out1 = ob_get_contents();
                ob_end_clean();
                $output .= $out1;
                $newPrefix = str_replace('<sup>|___</sup>', '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', $prefix).$spear;
            }
            $children = $Menu->children()->findAll(array('order'=>'orders ASC'));
            foreach($children as $child){
                $this->loopNode($output, $child,$Menu,$newPrefix,$spear);
            }
        }
    }
}
